﻿#include "font.h"
#include <assert.h>

#include <ft2build.h>

#include FT_FREETYPE_H



struct Font::Impl{
    FT_Library lib;


    FT_Face face;

    //FT_BYTE buf[128];

    Impl(){
        if ( FT_Init_FreeType( &lib)){
            assert(false);
        }


        if ( FT_New_Face( lib, "src/font/msyhbd.ttf", 0, &face)) {
            assert(false);
        }

        if ( FT_Set_Char_Size( face,  0, 16*64, 300, 300)) {
            assert(false);
        }
        if ( FT_Set_Pixel_Sizes( face, 0, 16) ){
            assert(face);
        }
        //if(FT_New_Memory_Face( lib, buf, 128 , 0, &face) ) {
        //assert(false);
        //}
    }

};


Font::Font():_impl(new Impl()){

}

Font::~Font(){

    delete _impl;
}


#ifdef UNIT_TEST
#include <iostream>

int main( int argc, char* argv[]){



    Font *font = new Font();

    std::cout<< font->_impl->face->num_faces<<std::endl;//how many faces, usualy 1

    std::cout<< font->_impl->face->num_glyphs<<std::endl;// home many glyphs contained.

    std::cout<< font->_impl->face->available_sizes<<std::endl;

    //int charcode = 0;
    //int glyph_index = FT_Get_Char_Index( font->_impl->face, charcode );//get the specified char face
    //std::cout<< "glyph_index: " << glyph_index<<std::endl;
    //
    //int load_flags = FT_LOAD_DEFAULT;//default
    //if( FT_Load_Glyph( font->_impl->face, glyph_index, load_flags ) ){ assert(false); }
    //if( FT_Render_Glyph( font->_impl->face->glyph,  FT_RENDER_MODE_MONO)){ assert(false); }
    //if( FT_Select_CharMap( font->_impl->face,  FT_ENCODING_BIG5 )){ assert(false); }//no support
    //


    int pen_x = 300; 
    int pen_y = 200; 

    //for( int i = 0 ; i < font->_impl->face->num_glyphs; ++i){

    FT_Long wc = L'我';
    //FT_Long wc = L'w';

    int error = FT_Load_Char( font->_impl->face, wc, FT_LOAD_RENDER ); 
    if ( error ){ std::cout<< "error ocurr!"<<std::endl; ;}

    //my_draw_bitmap( &font->_impl->face->glyph->bitmap, pen_x + font->_impl->face->glyph->bitmap_left , pen_y + font->_impl->face->glyph->bitmap_top);

    std::cout<< "bits: " << font->_impl->face->glyph<<std::endl;


    FT_Bitmap* bitmap= &font->_impl->face->glyph->bitmap;
    FT_Int x = font->_impl->face->glyph->bitmap_left;
    FT_Int y = font->_impl->face->glyph->bitmap_top;


    static const int WIDTH = 50;
    static const int HEIGHT= 50;
    FT_Int  i, j, p, q;
    FT_Int  x_max = x + bitmap->width;
    FT_Int  y_max = y + bitmap->rows;

    std::cout<< "width: "<<bitmap->width<<std::endl;
    std::cout<< "rows: " << bitmap->rows<<std::endl;
    std::cout<< "rows: " << bitmap->rows<<std::endl;
    std::cout<< "rows: " << bitmap->rows<<std::endl;

    unsigned char image[HEIGHT][WIDTH];
    memset( image, 0,  HEIGHT * WIDTH);

    for (int i = x, p = 0; i < x_max; i++, p++ )
    {
        for ( int j = y, q = 0; j < y_max; j++, q++ )
        {
            if ( i >= WIDTH || j >= HEIGHT )
            continue;

            image[j][i] |= bitmap->buffer[q * bitmap->width + p];
        }
    }

    std::cout<<"start draw.."<<std::endl;

    for ( i = 0; i < HEIGHT; i++ )
    {
        for ( j = 0; j < WIDTH; j++ )
            putchar( image[i][j] == 0 ? ' '
                    : image[i][j] < 128 ? '+'
                    : '*' );
        putchar( '\n' );
    }


    //font->_impl->face->glyph->advance.x >> 6 ;
    //}




    delete font;
    std::cout<<"done"<<std::endl;
    return 0;
}


#endif //UNIT_TEST
